package com.tianxinweb.controller;


import com.tianxinweb.entity.Privilege;
import com.tianxinweb.entity.Result;
import com.tianxinweb.enums.ResultEnum;
import com.tianxinweb.exception.MyException;
import com.tianxinweb.service.PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cboy
 * @since 2018-07-19
 */
@RestController
@RequestMapping("/privilege")
public class PrivilegeController {

    @Autowired
    private PrivilegeService privilegeService;

    /**
     * 获取所有权限
     * @return
     */
    @RequestMapping("list")
    public Result list(){
        int level = privilegeService.getPrivLevel();
        Map<String,List<Privilege>> map = new HashMap<>();
        for(int i=0;i<=level;i++){
            map.put("priv_"+i,privilegeService.getPrivByLevel(i));
        }
        return Result.success().add("level",level).add("privs", map);
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id, HttpServletRequest request){
        privilegeService.deleteById(id);
        //删除权限后重新把值赋予到ServletContext
        request.getServletContext().setAttribute("allPrivilege",privilegeService.selectList(null));
        return Result.success();
    }

    /**
     * 添加权限
     * @param privilege
     * @param result
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody @Valid Privilege privilege, BindingResult result, HttpServletRequest request){
        if(result.hasErrors()) {
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        privilegeService.addPrivilege(privilege);
        //添加权限后重新把值赋予到ServletContext
        request.getServletContext().setAttribute("allPrivilege",privilegeService.selectList(null));
        return Result.success();
    }

    /**
     * 修改权限
     * @param privilege
     * @param result
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody @Valid Privilege privilege,BindingResult result,HttpServletRequest request){
        if(result.hasErrors() || privilege.getPid()==null) {
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        privilegeService.updatePrivilege(privilege);
        //修改权限后重新把值赋予到ServletContext
        request.getServletContext().setAttribute("allPrivilege",privilegeService.selectList(null));
        return Result.success();
    }

    /**
     * 获取所有经过排序的菜单/权限
     * @return
     */
    @RequestMapping("getOrderedPriv")
    public Result getOrderedPrivilege(@RequestParam(value = "type",defaultValue = "0") Integer type){
        List<Privilege> list = privilegeService.getOrderedPrivilege(type);
        return Result.success().add("data", list);
    }


    /**
     * 通过id获取权限
     * @param pid
     * @return
     */
    @RequestMapping("getPrivById/{pid}")
    public Result getPrivilegeById(@PathVariable("pid") Integer pid){
        Privilege privilege = privilegeService.selectById(pid);
        return Result.success().add("privilege",privilege);
    }

}

